package com.intellagric.service.impl.module.sys_user_scene;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftEquipmentMapper;
import com.intellagric.mapper.IftSceneMapper;
import com.intellagric.mapper.SysUserSceneMapper;
import com.intellagric.pojo.*;
import com.intellagric.service.module.sys_user_scene.UserSceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/6/3
 * @DESC： 用户场景管理服务
 */
@Service
public class UserSceneServiceImpl implements UserSceneService {

    @Autowired
    private SysUserSceneMapper userSceneMapper;

    @Autowired
    private IftSceneMapper sceneMapper;

    @Autowired
    private IftEquipmentMapper equipmentMapper;


    /**
     * 添加用户关联的场景
     * @param userSceneKey
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addUserScene(SysUserSceneKey userSceneKey) {
        //删除用户原有的场景
        SysUserSceneExample userSceneExample = new SysUserSceneExample();
        userSceneExample.createCriteria().andUserIdEqualTo(userSceneKey.getUserId());
        userSceneMapper.deleteByExample(userSceneExample);

        //添加新的用户场景信息
        String[] sceneIds = userSceneKey.getSceneIds();
        SysUserSceneKey sysUserSceneKey = new SysUserSceneKey();
        String userId = userSceneKey.getUserId();
        boolean flag = true;
        for (String sceneId : sceneIds) {
            sysUserSceneKey.setUserId(userId);
            sysUserSceneKey.setSceneId(sceneId);
            int insert = userSceneMapper.insert(sysUserSceneKey);
            if (insert != 1) {
                flag = false;
            }
        }
        //返回添加结果
        if (flag) {
            return ResponseMessage.success();
        }
        return ResponseMessage.fail();
    }

    /**
     * 根据用户id查询场景信息列表
     * @param userId
     * @return SysUserSceneKey
     */
    @Override
    public SysUserSceneKey getUSerSceneByUserId(String userId) {
        SysUserSceneExample userSceneExample = new SysUserSceneExample();
        userSceneExample.createCriteria().andUserIdEqualTo(userId);
        List<SysUserSceneKey> sysUserSceneKeys = userSceneMapper.selectByExample(userSceneExample);

        //封装用户场景信息到SysUSerSceneKey中
        SysUserSceneKey userSceneKey = new SysUserSceneKey();
        String[] sceneIds = new String[sysUserSceneKeys.size()];
        for (int i = 0; i < sysUserSceneKeys.size(); i++) {
            sceneIds[i] = sysUserSceneKeys.get(i).getSceneId();
        }
        userSceneKey.setSceneIds(sceneIds);
        return userSceneKey;
    }


    /**
     * 根据用户id查询用户场景信息，封装成树形数据
     * @param userId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getUserScene(String userId) {
        //根据用户id查询场景信息
        SysUserSceneKey userSceneKey = getUSerSceneByUserId(userId);
        List<String> sceneIds = new ArrayList<>();
        for (String sceneId : userSceneKey.getSceneIds()) {
            sceneIds.add(sceneId);
        }
        IftSceneExample sceneExample1 = new IftSceneExample();
        sceneExample1.createCriteria().andIdIn(sceneIds);
        List<IftScene> sceneList = sceneMapper.selectByExample(sceneExample1);

        IftSceneExample sceneExample2 = new IftSceneExample();
        sceneExample2.setOrderByClause("sort");
        sceneExample2.createCriteria().andParentIdEqualTo("0").andIdIn(sceneIds);
        List<IftScene> rootsceneList = sceneMapper.selectByExample(sceneExample2);

        for (IftScene scene : rootsceneList ) {
            scene.setChildren(getChild(scene.getId(),sceneList));
        }
        return LayuiDataGridResult.success().add(rootsceneList,rootsceneList.size());
    }

    /**
     * 根据用户场景信息查询用户设备的列表
     * @param userId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getUserSceneEquipment(String userId) {
        //根据用户id查询场景列表
        SysUserSceneKey userSceneKey = getUSerSceneByUserId(userId);
        List<String> sceneIds = new ArrayList<>();
        for (String sceneId : userSceneKey.getSceneIds()) {
            sceneIds.add(sceneId);
        }
        IftEquipmentExample equipmentExample = new IftEquipmentExample();
        equipmentExample.createCriteria().andSceneIdIn(sceneIds);
        List<IftEquipment> userEquipmentList = equipmentMapper.selectByExample(equipmentExample);
        return LayuiDataGridResult.success().add(userEquipmentList,userEquipmentList.size());
    }

    /**
     * 递归查询子分类
     * @param id
     * @param sceneList
     * @return ResponseMessage
     */
    private List<IftScene> getChild(String id, List<IftScene> sceneList) {

        //返回的子菜单
        List<IftScene> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (IftScene scene : sceneList) {
            if (scene.getParentId() != null) {
                //如果列表中的父id等于当前的分类的id，那就加入到该分类子列表中
                if (scene.getParentId().equals(id)) {
                    childList.add(scene);
                }
            }
        }

        // 把子菜单的子菜单childList再循环一遍
        for (IftScene scene : childList) {
            scene.setChildren(getChild(scene.getId(),sceneList));
        }

        //退出递归的条件
        if (childList.size() == 0) {
            return null;
        }

        return childList;
    }

}
